import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:perfect_volume_control/perfect_volume_control.dart';

class VolumeTestWidget extends StatefulWidget {
  const VolumeTestWidget({Key? key}) : super(key: key);

  @override
  State<VolumeTestWidget> createState() => _VolumeTestWidgetState();
}

class _VolumeTestWidgetState extends State<VolumeTestWidget> {
  double volume = 0;
  late StreamSubscription<double> _subscription;

  @override
  initState() {
    super.initState();
    _subscription = PerfectVolumeControl.stream.listen((volume) {
      print('当前volume:$volume');
    });
  }

  @override
  void dispose() {
    super.dispose();
    _subscription.cancel();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Volume',
      home: Scaffold(
        appBar: AppBar(title: Text('Volume 音量测试')),
        body: Container(
          child: CupertinoButton(
              child: Text('获取音量>>> volume:$volume'),
              onPressed: () async {
                volume = await PerfectVolumeControl.getVolume();
                print('>>>volume:$volume');
                setState(() {});
              }),
        ),
      ),
    );
  }
}
